package com.five.matebook.activity.columeedit;

import android.content.Context;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;

import com.five.matebook.activity.db.CustomValueTable;
import com.five.matebook.activity.db.OneQueryData;
import com.five.matebook.activity.db.QueryFilter;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class CustomValueManager {
    public static final String FILED_USAGE_OUTCOME = "filed_usage_outcome";
    public static final String FILED_ACCOUNT_OUTCOME = "filed_account_outcome";
    public static final String FILED_ROLE_OUTCOME = "filed_role_outcome";
    public static final String FILED_PROJECT_OUTCOME = "filed_project_outcome";

    public static final String FILED_USAGE_INCOME = "filed_usage_income";
    public static final String FILED_ACCOUNT_INCOME = "filed_account_income";
    public static final String FILED_ROLE_INCOME = "filed_role_income";
    public static final String FILED_PROJECT_INCOME = "filed_project_income";
    private static final String TAG = "CustomValueManager";

    private static CustomValueManager sInstace = null;
    public static CustomValueManager getInstance(Context context) {
        if (sInstace == null) {
            synchronized (CustomValueManager.class) {
                if (sInstace == null) {
                    sInstace = new CustomValueManager(context);
                }
            }
        }
        return sInstace;
    }

    private Context mContext;
    private CustomValueManager(Context context) {
        this.mContext = context;
    }

    public List<String> getFiledValues(String filedType) {
        QueryFilter filter = new QueryFilter.Builder()
                .addStringMatch(CustomColume.COLUME_KEY, filedType, true)
                .build();
        List<OneQueryData> data = CustomValueTable.getInstance(mContext).query(filter);
        if (data.size() > 0) {
            String value = data.get(0).get(CustomColume.COLUME_VALUE);
            Gson gson = new Gson();
            String[] arr = gson.fromJson(value, String[].class);
            return Arrays.asList(arr);
        }
        return null;
    }

    public boolean setFiledValues(String filedType, List<String> values) {
        if (values == null || values.size() == 0) {
            Log.e(TAG, "setFiledValues() failed, values is null");
            return false;
        }
        if (TextUtils.isEmpty(filedType)) {
            Log.e(TAG, "setFiledValues() filedType is null");
            return false;
        }
        Map<String, Object> m = new ArrayMap<>();
        Gson gson = new Gson();
        m.put(CustomColume.COLUME_KEY, filedType);
        m.put(CustomColume.COLUME_VALUE, gson.toJson(values));

        QueryFilter filter = new QueryFilter.Builder()
                .addStringMatch(CustomColume.COLUME_KEY, filedType, true)
                .build();
        List<OneQueryData> data = CustomValueTable.getInstance(mContext).query(filter);
        if (data != null && data.size() > 0) {
            // 更新
            CustomValueTable.getInstance(mContext).update(data.get(0).getLong("id", 0), m);
        } else {
            // 新增
            CustomValueTable.getInstance(mContext).add(m);
        }
        return true;
    }
}
